package com.td.erhome.ui.min;

import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.td.erhome.R;
import com.td.erhome.adapter.BugHouseListAdapter;
import com.td.erhome.base.BaseActivity;
import com.td.erhome.models.BaseBean;
import com.td.erhome.models.HouseBean;
import com.td.erhome.net.Api;
import com.td.erhome.net.BaseSubscriber;
import com.td.erhome.utils.UserManager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 我的收藏。浏览记录，
 */
public class MyCollectActivity extends BaseActivity {

    @Bind(R.id.img_back)
    ImageView mImgBack;
    @Bind(R.id.tv_title)
    TextView mTvTitle;
    @Bind(R.id.recyclview)
    XRecyclerView mRecyclerView;
    @Bind(R.id.tv_error)
    TextView mTvError;
    private List<HouseBean.SellHouseListBean> mhouse_list = new ArrayList<>();
    private BugHouseListAdapter mAdapter;
    private int page = 1;
    private String mType;
    private int mTotal_count;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_my_collect;
    }

    @Override
    protected void initView() {
        mType = getIntent().getStringExtra("type");
        mTvTitle.setText(mType);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        mAdapter = new BugHouseListAdapter(this,mhouse_list);
        mRecyclerView.setAdapter(mAdapter);

        mRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                mAdapter.clearData();
                page =1;
                getData();
            }
            @Override
            public void onLoadMore() {
                if (mTotal_count >mAdapter.mhouse_list.size()){
                    page++;
                    getData();
                }else {
                    mRecyclerView.refreshComplete();
                }

            }
        });

        getData();

    }

    @Override
    protected void releaseResource() {
    }



    @OnClick({R.id.img_back,R.id.tv_error})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.tv_error:
                mAdapter.clearData();
                page =1;
                getData();
                break;
        }
    }

    public void getData(){
        HashMap<String, Object> params = new HashMap<>();
        params.put("user_id", UserManager.getIns().getUser().uid+"");
        params.put("page", page);
        if (mType.equals("我的收藏")) getFavoriteList(params);
        if (mType.equals("我的浏览记录")) getReviewInfoList(params);
        if (mType.equals("看房记录")) getBuyHouseList(params);
    }


     //我的浏览记录
    private void getReviewInfoList(HashMap<String, Object> params) {
        Api.getRetrofit().getReviewInfoList(params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseSubscriber<BaseBean<HouseBean>>(MyCollectActivity.this){
                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                        mRecyclerView.refreshComplete();
                    }
                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        mRecyclerView.refreshComplete();
                        mTvError.setVisibility(View.VISIBLE);
                    }
                    @Override
                    public void onNext(BaseBean<HouseBean> houseBeanBaseBean) {
                        super.onNext(houseBeanBaseBean);
                        mTotal_count = houseBeanBaseBean.getData().total_count;
                        mhouse_list = houseBeanBaseBean.getData().house_list;
                        mAdapter.addData(mhouse_list);
                        mRecyclerView.refreshComplete();

                        if (mAdapter.mhouse_list.size()==0){
                            mTvError.setVisibility(View.VISIBLE);
                        }else {
                            mTvError.setVisibility(View.GONE);
                        }
                    }
                });
    }

    //我的收藏
    private void getFavoriteList(HashMap<String, Object> params) {
        Api.getRetrofit().getFavoriteList(params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseSubscriber<BaseBean<HouseBean>>(MyCollectActivity.this){
                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                        mRecyclerView.refreshComplete();
                    }
                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        mRecyclerView.refreshComplete();
                        mTvError.setVisibility(View.VISIBLE);
                    }
                    @Override
                    public void onNext(BaseBean<HouseBean> houseBeanBaseBean) {
                        super.onNext(houseBeanBaseBean);
                        mTotal_count = houseBeanBaseBean.getData().total_count;
                        mhouse_list = houseBeanBaseBean.getData().house_list;
                        mAdapter.addData(mhouse_list);
                        mRecyclerView.refreshComplete();
                        if (mAdapter.mhouse_list.size()==0){
                            mTvError.setVisibility(View.VISIBLE);
                        }else {
                            mTvError.setVisibility(View.GONE);
                        }
                    }
                });
    }

    //看房记录
    private void getBuyHouseList(HashMap<String, Object> params) {
        Api.getRetrofit().getBuyHouseList(params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseSubscriber<BaseBean<HouseBean>>(MyCollectActivity.this){
                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                        mRecyclerView.refreshComplete();
                    }
                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        mRecyclerView.refreshComplete();
                        mTvError.setVisibility(View.VISIBLE);
                    }
                    @Override
                    public void onNext(BaseBean<HouseBean> houseBeanBaseBean) {
                        super.onNext(houseBeanBaseBean);
                        mTotal_count = houseBeanBaseBean.getData().total_count;
                        mhouse_list = houseBeanBaseBean.getData().house_list;
                        mAdapter.addData(mhouse_list);
                        mRecyclerView.refreshComplete();
                        if (mAdapter.mhouse_list.size()==0){
                            mTvError.setVisibility(View.VISIBLE);
                        }else {
                            mTvError.setVisibility(View.GONE);
                        }
                    }
                });
    }

}
